package system;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Minutes;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class FieldValidator {
	
	/**
	 * This method checks fields and return TRUE if they contain only digits, and FALSE otherwise. 
	 */
	public static boolean checkNumeric(String num){
		if (num.matches("^[0-9]+$") && num.length()>0)
			return true;
		return false;
	}
	
	/**
	 * This method checks fields and return TRUE if they contain a valid Email address, and FALSE otherwise. 
	 */
	public static boolean checkEmail(String email){
		if (email.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
				+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$") && email.length()>0)
			return true;
		return false;
	}
	
	/**
	 * This method checks fields and return TRUE if they contain a valid parking space number, and FALSE otherwise. 
	 */
	public static boolean checkSpaceNumber(String space){
		if (space.matches("^[1-8]+$") && space.length()==1)
			return true;
		return false;
	}
	
	/**
	 * This method checks fields and return TRUE if they contain a valid price, and FALSE otherwise. 
	 */
	public static boolean checkPrice(String price){
		if (price.matches("^([0-9][0-9]?([,][0-9]{3}){0,4}([.][0-9]{0,4})?)$|^[$]?([0-9]{1,14})?([.][0-9]{1,4})$|^[$]?[0-9]{1,14}$") && price.length()>0)
			return true;
		return false;
	}
	
	/**
	 * This method checks fields and return TRUE if they contain a IP, and FALSE otherwise. 
	 */
	public static boolean checkIP(String ip){
		if (/*ip.matches("^(2[0-5][0-5])|(1\\d\\d)|([1-9]?\\d)\\.){3}(2[0-5][0-5])|(1\\d\\d)|([1-9]?\\d)$") && */ip.length()>0)
			return true;
		return false;
	}
	
	/**
	 * This method checks fields and return TRUE if they contain a valid port number, and FALSE otherwise. 
	 */
	public static boolean checkPort(String port){
		if (port.matches("^[0-9]+$") && port.length()>0 && port.length()<5)
			return true;
		return false;
	}
	
	/**
	 * This method checks fields and return TRUE if they contain a valid time, and FALSE otherwise. 
	 */
	public static boolean checkTime(String time){
		if (time.matches("^([01]?[0-9]|2[0-3]):[0-5][0-9]") && time.length()==5)
			return true;
		return false;
	}
	
	/**
	 * This method checks fields and return TRUE if they contain a valid date, and FALSE otherwise. 
	 */
	public static boolean checkDate(Date date){
		DateTime check = new DateTime(date);
		DateTime now = new DateTime();
		
		if (Days.daysBetween(now, check).getDays()>=0)
			return true;
		else return false;
	}
}
